package cn.yitun.chat.yitunserverimapicore.business.group.action;

import cn.yitun.chat.yituncommonmessage.ExceptionEnum;
import cn.yitun.chat.yituncommonmessage.YitunException;
import cn.yitun.chat.yituncommonmessage.impl.HeadImpl;
import cn.yitun.chat.yituncommonmessage.result.ResultMessage;
import cn.yitun.chat.yituncommonutil.YitunStringUtil;
import cn.yitun.chat.yitungeneral.annotation.action.ActionMapping;
import cn.yitun.chat.yitungeneral.annotation.parameter.JsonDefine;
import cn.yitun.chat.yitunnetsession.session.SocketSession;
import cn.yitun.chat.yitunservercommonlib.constant.ActionTypeConstant;
import cn.yitun.chat.yitunservercommonlib.dataentity.dto.group.GroupCategoryDto;
import cn.yitun.chat.yitunservercommonlib.util.NumUtil;
import cn.yitun.chat.yitunservercommonlib.util.R;
import cn.yitun.chat.yitunserverimapicore.business.group.service.GroupCategoryService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 
 * Date 2019-01-20 21:12:18<br>
 * Description
 * 
 * @author yitun
 * @since 1.0.0
 */
@Component
@ActionMapping(value = ActionTypeConstant.GROUP_CATEGORY_ACTION_PUSH)
public class GroupCategoryAction {
	@Resource
	private GroupCategoryService groupCategorySerivce;

	/**
	 * Date 2019-01-22 19:36:14<br>
	 * Description 获取分组数量
	 * @author yitun
	 * @param socketSession
	 * @return
	 * @since 1.0.0
	 */
	@ActionMapping(value = "1.1.0001")
	public ResultMessage count(SocketSession socketSession) {
		Long userId = socketSession.getKey();
		return new ResultMessage(R.param("count", groupCategorySerivce.count(R.param("userId", userId))));
	}

	/**
	 * Date 2019-01-20 14:28:43<br>
	 * Description 获取群分组列表
	 * @author yitun
	 * @param socketSession
	 * @return
	 * @since 1.0.0
	 */
	@ActionMapping(value = "1.1.0002")
	public ResultMessage list(SocketSession socketSession) {
		Long userId = socketSession.getKey();
		return new ResultMessage(R.param("list", groupCategorySerivce.getListByUserId(userId)));
	}

	/**
	 * Date 2019-01-20 14:22:21<br>
	 * Description 新增群分组
	 * @author yitun
	 * @param socketSession
	 * @param query
	 * @return
	 * @since 1.0.0
	 */

	@ActionMapping(value = "1.1.0003")
	public ResultMessage get(SocketSession socketSession, @JsonDefine("data") GroupCategoryDto query) {
		Long userId = socketSession.getKey();
		return new ResultMessage(R.param("category", groupCategorySerivce.getVOById(userId, query.getId())));
	}

	@ActionMapping(value = "1.1.0004")
	public ResultMessage add(
			SocketSession socketSession,
			HeadImpl head,
			@JsonDefine("data") GroupCategoryDto groupCategory) {
		if (NumUtil.isInValid(groupCategory.getUserId()))
			groupCategory.setUserId(socketSession.getKey());

		return groupCategorySerivce.add(head.getKey(), groupCategory);
	}

	/**
	 * 修改分组名
	 * 
	 * @param socketSession
	 * @param query
	 * @return
	 */
	@ActionMapping(value = "1.1.0006")
	public ResultMessage updateName(
			SocketSession socketSession,
			HeadImpl head,
			@JsonDefine("data") GroupCategoryDto query) {
		if (NumUtil.isInValid(query.getId()) || YitunStringUtil.isEmpty(query.getName()))
			throw new YitunException(ExceptionEnum.LOSE_FILED);

		query.setUserId(socketSession.getKey());
		return groupCategorySerivce.updateName(head.getKey(), query);
	}

	@ActionMapping(value = "1.1.0007")
	public ResultMessage updateRank(
			SocketSession socketSession,
			HeadImpl head,
			@JsonDefine("data") GroupCategoryDto query) {
		if (NumUtil.isInValid(query.getId()) || query.getRank() == null)
			throw new YitunException(ExceptionEnum.LOSE_FILED);

		query.setUserId(socketSession.getKey());
		return groupCategorySerivce.updateRank(head.getKey(), query);
	}

	@ActionMapping(value = "1.1.0008")
	public ResultMessage delete(
			SocketSession socketSession,
			HeadImpl head,
			@JsonDefine("data") GroupCategoryDto query) {

		if (NumUtil.isInValid(query.getId()))
			throw new YitunException(ExceptionEnum.LOSE_FILED);

		query.setUserId(socketSession.getKey());
		return groupCategorySerivce.delete(head.getKey(), query);
	}
}
